//
//  ClassAndStruct.swift
//  cnswift-demo
//
//  Created by YangWei on 2020/12/28.
//  Copyright © 2020 YangWei. All rights reserved.
//

import UIKit

/// 显示器分辨率
struct Resolution {
    var width = 0
    var height = 0
}

/// 视频显示模式
class VideoMode {
    // 分辨率
    var resolution = Resolution()
    /*
     Interlaced 隔行 扫描
     Interlaced（ 隔行 扫描）是一种视频存储模式一个交错的视频流不包含帧而是一些场，
     每个场包含了一帧里的一半数量的行（全部是odd或全部是even）。
     */
    var interlaced = false
    // 帧率
    var frameRate = 0.0
    var name: String?
}

/// 指南针的四个主要方向
enum CompassPointV3 {
    case North, South, East, West
}

class StructuresAndClasses: SWDBaseObject {
    
    class func run() {
        
        let structuresAndClasses = StructuresAndClasses()
        
        // 1.1
        structuresAndClasses.definitionSyntax()
        
        // 1.2
        structuresAndClasses.structureAndClassInstances()
        
        // 1.3
        structuresAndClasses.accessingProperties()
        
        // 1.4
        structuresAndClasses.memberwiseInitializersForStructureTypes()
        
        // 2
        structuresAndClasses.structuresAndEnumerationsAreValueTypes()
        
        // 3
        structuresAndClasses.classesAreReferenceTypes()
        
        // 3.1
        structuresAndClasses.identityOperators()
    }
    
    /// 1.1 定义语法
    func definitionSyntax() {
        let title = "1.1 定义语法";
        printStartLine(title)
        
        // 参见 Resolution 和 VideoMode 的定义
        
        printEndLine(title)
    }
    
    /// 1.2 类与结构体实例
    func structureAndClassInstances() {
        let title = "1.2 类与结构体实例";
        printStartLine(title)
        
        let someResolution = Resolution()
        print(someResolution)
        
        let someVideoMode = VideoMode()
        print(someVideoMode)
        
        printEndLine(title)
    }
    
    /// 1.3 访问属性
    func accessingProperties() {
        let title = "1.3 访问属性";
        printStartLine(title)
        
        /* 用点语法来访问一个实例的属性 */
        let someResolution = Resolution()
        print("someResolution 的宽度为 \(someResolution.width)")
        // someResolution 的宽度为 0
        
        /* 访问子属性，如 VideoMode中 resolution属性中的 width属性：*/
        let someVideoMode = VideoMode()
        print("someVideoMode 的宽度为 \(someVideoMode.resolution.width)")
        // someVideoMode 的宽度为 0
        
        /* 用点语法对一个变量属性赋新值：*/
        someVideoMode.resolution.width = 1280
        print("someVideoMode 的宽度现在是 \(someVideoMode.resolution.width)")
        // someVideoMode 的宽度现在是 1280
        
        printEndLine(title)
    }
    
    /// 1.4 结构体类型的成员初始化器
    func memberwiseInitializersForStructureTypes() {
        let title = "1.4 结构体类型的成员初始化器";
        printStartLine(title)
        
        let vga = Resolution(width: 640, height: 480)
        print(vga) // Resolution(width: 640, height: 480)
        
        printEndLine(title)
    }
    
    /// 2 结构体和枚举是值类型
    func structuresAndEnumerationsAreValueTypes() {
        let title = "2 结构体和枚举是值类型";
        printStartLine(title)
        
        let hd = Resolution(width: 1920, height: 1080)
        var cinema = hd
        
        cinema.width = 2048

        print("cinema 现在有 \(cinema.width) 像素宽")
        // cinema 现在有 2048 像素宽

        print("hd 仍然是 \(hd.width) 像素宽")
        // hd 仍然是 1920 像素宽
        
        /* 值拷贝同样适用于枚举 */
        var currentDirection = CompassPointV3.West
        let rememberedDirection = currentDirection
        currentDirection = .East
        if rememberedDirection == .West {
            print("记忆中的方向仍然是 .West")
        }
        // 记忆中的方向仍然是 .West
        
        printEndLine(title)
    }
    
    /// 3 类是引用类型
    func classesAreReferenceTypes() {
        let title = "3 类是引用类型";
        printStartLine(title)
        
        let hd = Resolution(width: 1920, height: 1080)
        
        /* 不同于值类型，引用类型被赋值到一个常量，变量或者本身被传递到一个函数的时候是不会被拷贝的。*/
        let tenEighty = VideoMode()
        tenEighty.resolution = hd
        tenEighty.interlaced = true
        tenEighty.name = "1080i"
        tenEighty.frameRate = 25.0

        let alsoTenEighty = tenEighty
        alsoTenEighty.frameRate = 30.0
        
        print("tenEighty 的帧速率特性现在是 \(tenEighty.frameRate)")
        // tenEighty 的帧速率特性现在是 30.0
        
        printEndLine(title)
    }
    
    /// 3.1 特征运算符
    func identityOperators() {
        let title = "3.1 特征运算符";
        printStartLine(title)
        
        let hd = Resolution(width: 1920, height: 1080)
        
        let tenEighty = VideoMode()
        tenEighty.resolution = hd
        tenEighty.interlaced = true
        tenEighty.name = "1080i"
        tenEighty.frameRate = 25.0

        let alsoTenEighty = tenEighty
        alsoTenEighty.frameRate = 30.0
        
        /* === (!==)检查两个常量或者变量是否引用相同的实例（值相同且引用相同）*/
        if tenEighty === alsoTenEighty {
            print("tenEighty 和 alsoTenEighty 指的是同一个 VideoMode 实例。")
        }
        // tenEighty 和 alsoTenEighty 指的是同一个 VideoMode 实例。
        
        printEndLine(title)
    }
}
